﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using LowLevelGraphics;

namespace LowLevelGraphics.Drawing.FloodFillModes
{
    /// <summary>
    /// 
    /// </summary>
    public class FloodFill2
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_x"></param>
        /// <param name="_y"></param>
        public void FloodFill22(UnsafeBitmap _bitmap, int _x, int _y, Color _color)
        {
            int max = 0;
            bool flag = false;
            int xx = 0;
            int x1 = 0;
            int maxX = 200;
            int maxY = 200;
            int y = 0;
            int x = 0;
            int nWidth = _bitmap.Width;
            int nHeight = _bitmap.Height;
            bool[] BeschX = new bool[nWidth];
            bool[] BeschY = new bool[_bitmap.Height];

            int[,] Pixel = new int[nWidth, nHeight];
            int neu = 0; // Anzahl neuer Pixel
            for (y = 0; y < maxY; y++)
            {
                if (BeschX[y] == true)// Zeile untersuchen?
                {
                    BeschX[y] = false; // setze zurück
                    flag = false;      // Grenzübergang stattgefunden?
                    x1 = 0;          // Position des Grenzübergangs
                    x = 0;
                    while (x <= maxX)
                    {
                        if (Pixel[x, y] != 0)
                        {
                            if (flag)
                            {
                                // Fülle von Pixel[x1,y] bis Pixel[x-1,y]
                                xx = x1;
                                while (xx < x)
                                {
                                    Pixel[xx, y] = 1;
                                    BeschY[xx] = true;
                                    neu = neu + 1;
                                    xx = xx + 1;
                                }
                                flag = false;
                            }
                            while (x + 1 <= maxX && Pixel[x + 1, y] == 0)
                            {
                                Pixel[x + 1, y] = 1;
                                BeschY[x + 1] = true;
                                neu = neu + 1;
                                x = x + 1;
                            }
                        }
                        if (flag == false)
                        {
                            x1 = x;
                            flag = true;
                        }
                    }
                    flag = false;

                    x = x + 1;
                }
                if (neu == 0)
                    break;
            }

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    if (Pixel[x, y] > 0)
                    {
                        _bitmap.SetPixel(x, y, Color.Red);
                    }
                }
            }
        }
    }
}
